# 1、读取分数的数据
with open(file="../../data/score.txt", encoding="utf-8", mode="r") as f:
    lines = f.readlines()

print(lines)

# 2、统计学生的总分
sum_score = {}
for line in lines:
    # 去掉后面的换行符，按照逗号切分
    split = line.strip().split(",")
    # 学号
    id = split[0]
    # 分数
    score = int(split[-1])
    # 累加计算总分
    # 如果学号在字典中存在，累计分数，如果不存在存入当前分数
    if id in sum_score:
        sum_score[id] += score
    else:
        sum_score[id] = score

print(sum_score)

# 3、将统计结果保存到文件中
with open(file="../../data/sum_score.txt", encoding="utf-8", mode="w") as f:
    for id, score in sum_score.items():
        f.write(f"{id},{score}\n")



# 3、取出总分前十的学生
# 将字典转换成列表，字典不能排序
sum_score_list = list(sum_score.items())

print(sum_score_list)

n = len(sum_score_list)
# 按照总分排序
for i in range(n - 1):
    for j in range(n - (i + 1)):
        # 如果前面的分数小于后面的分数，交换位置
        if sum_score_list[j][1] < sum_score_list[j + 1][1]:
            sum_score_list[j], sum_score_list[j + 1] = sum_score_list[j + 1], sum_score_list[j]

# 取前十
top10 = sum_score_list[:10]
print(top10)

# 3、将统计结果保存到文件中
with open(file="../../data/top10.txt", encoding="utf-8", mode="w") as f:
    for id, score in top10:
        f.write(f"{id},{score}\n")